package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.OrderBean;
import com.project.bean.ShoppingCarBean;
import com.project.mapper.IInfoMapper;
import com.project.mapper.IOrderMapper;
import com.project.mapper.IShopMapper;
import com.project.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Transactional
@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private IOrderMapper orderMapper;
    @Autowired
    private IShopMapper shopMapper;
    @Autowired
    private IInfoMapper infoMapper;
    @Override
    public void addOrder(OrderBean order) {
        //用户购物车集合
       List<ShoppingCarBean> shop =shopMapper.findCars(order.getUserId());
       //得到订单总价
       order.setMoney(this.getTotalPrice(shop));
       //添加对象
       orderMapper.insert(order);

        //添加订单明细
    infoMapper.addInfoList(order.getId(),shop);

    //按用户ID清空购物车
    QueryWrapper<ShoppingCarBean> qw = new QueryWrapper<>();
    qw.eq("fk_userId",order.getUserId());
    shopMapper.delete(qw);
    }

    private Integer getTotalPrice(List<ShoppingCarBean> shopList){
        int total=0;
        for (ShoppingCarBean shop:shopList){
            total += shop.getBuyNum() * shop.getProduct().getPrice();
        }
        return total;
    }

    @Override
    public IPage<OrderBean> findByUser(Integer pageNO, Integer userId) {
        QueryWrapper<OrderBean> qw=new QueryWrapper<>();
        qw.eq("fk_userId",userId);
        qw.orderByDesc("o_date");
        return orderMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public OrderBean findOrder(Integer orderId) {
        OrderBean order = orderMapper.selectById(orderId);
        order.setInfoBeans(infoMapper.getinfo(orderId));
        return order;
    }

    @Override
    public IPage<OrderBean> selectByItems(Integer pageNO, String status, LocalDate startDate, LocalDate endDate) {
        QueryWrapper<OrderBean> qw = new QueryWrapper<>();
        if (!(status.equals("不限"))){
            qw.eq("o_status",status);
        }
        if (startDate != null){
            qw.ge("o_date",startDate);
        }
        if (endDate != null){
            qw.le("o_date",endDate);
        }
        return orderMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public void sendOrder(Integer orderId) {
        UpdateWrapper<OrderBean> uw = new UpdateWrapper<>();
        uw.eq("pk_orderId",orderId);
        uw.set("o_status","发货中");
        orderMapper.update(null,uw);
    }

    @Override
    public List<OrderBean> findSending() {
        QueryWrapper<OrderBean> qw = new QueryWrapper<>();
        qw.eq("o_status","发货中");
        return orderMapper.selectList(qw);
    }

    @Override
    public void changeStatus(Integer orderId) {
        UpdateWrapper<OrderBean> uw = new UpdateWrapper<>();
        uw.eq("pk_orderId",orderId);
        uw.set("o_status","已完结");
        orderMapper.update(null,uw);
    }
}
